#include "head.h"

int tcpInit(const char* ip,const char* port,int* sockfd){

    *sockfd=socket(AF_INET,SOCK_STREAM,0);//分配socket文件描述符
    int reuse=1;
    int ret=setsockopt(*sockfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse));//允许重用sockfd

    if(ret==-1){

        error(1,errno,"setsockopt is error\n");
    }

    struct sockaddr_in addr;
    addr.sin_family=AF_INET;
    addr.sin_port=htons(atoi(port));
    addr.sin_addr.s_addr=inet_addr(ip);
    ret=bind(*sockfd,(struct sockaddr*)&addr,sizeof(addr));
    if(ret==-1){

        error(1,errno,"bind is error\n");
    }

    listen(*sockfd,50);//监听等待客户端链接
    return 0;
    

}
